﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Collections;
using System.Security.Cryptography;
using System.Data;
using DTO;

namespace DAL
{
    public class TKDangNhapDAO
    {
        private MyConnection myConnection;

        public TKDangNhapDAO()
        {
            this.myConnection = DataAccessObject.GetDAL();
        }

        /// 
        /// <param name="condition"></param>
        /// <param name="orderBy"></param>
        public List<TKDangNhapDTO> Select(string condition, string orderBy)
        {
            SqlDataReader reader = this.myConnection.Select("dbo.TKDANGNHAP", condition, orderBy);
            List<TKDangNhapDTO> resultList = new List<TKDangNhapDTO>();
            while (reader.Read())
            {
                resultList.Add(ConvertFromDataReader((IDataReader)reader));
            }
            return resultList;
        }

        /// 
        /// <param name="taiKhoan"></param>
        public void Insert(TKDangNhapDTO taiKhoan)
        {
            List<DataStruct> dataStruct = new List<DataStruct>();
            dataStruct.Add(new DataStruct("MaTaiKhoan", ""));
            dataStruct.Add(new DataStruct("TenDangNhap", taiKhoan.Username));
            dataStruct.Add(new DataStruct("MatKhau", EncodePasswordToBase64(taiKhoan.Password)));
            dataStruct.Add(new DataStruct("MaNhanVien", taiKhoan.MaNhanVien));
            dataStruct.Add(new DataStruct("TrangThai", taiKhoan.TrangThai));
            this.myConnection.Insert("dbo.TKDANGNHAP", dataStruct);
        }

        /// 
        /// <param name="taiKhoan"></param>
        public void UpdateInfo(TKDangNhapDTO taiKhoan)
        {
            List<DataStruct> dataStruct = new List<DataStruct>();
            dataStruct.Add(new DataStruct("TenDangNhap", taiKhoan.Username));
            dataStruct.Add(new DataStruct("MaNhanVien", taiKhoan.MaNhanVien));
            dataStruct.Add(new DataStruct("TrangThai", taiKhoan.TrangThai));
            this.myConnection.Update("dbo.TKDANGNHAP", dataStruct, "MaTaiKhoan = '" + taiKhoan.MaTaiKhoan + "'");
        }

        /// 
        /// <param name="taiKhoan"></param>
        public void UpdatePassword(TKDangNhapDTO taiKhoan)
        {
            List<DataStruct> dataStruct = new List<DataStruct>();
            dataStruct.Add(new DataStruct("MatKhau", EncodePasswordToBase64(taiKhoan.Password)));
            this.myConnection.Update("dbo.TKDANGNHAP", dataStruct, "MaTaiKhoan = '" + taiKhoan.MaTaiKhoan + "'");
        }

        /// 
        /// <param name="taiKhoan"></param>
        public void Delete(TKDangNhapDTO taiKhoan)
        {
            this.myConnection.Delete("dbo.TKDANGNHAP", "MaTaiKhoan = '" + taiKhoan.MaTaiKhoan + "'");
        }

        private TKDangNhapDTO ConvertFromDataReader(IDataReader dataReader)
        {
            TKDangNhapDTO taiKhoan = new TKDangNhapDTO();
            taiKhoan.MaTaiKhoan = dataReader["MaTaiKhoan"].ToString();
            taiKhoan.Username = dataReader["TenDangNhap"].ToString();
            taiKhoan.Password = null;
            taiKhoan.MaNhanVien = DBNull.Value.Equals(dataReader["MaNhanVien"]) ? null : dataReader["MaTaiKhoan"].ToString();
            taiKhoan.TrangThai = bool.Parse(dataReader["TrangThai"].ToString());
            return taiKhoan;
        }

        public string EncodePasswordToBase64(string password)
        {
            try
            {
                byte[] bytes = Encoding.Unicode.GetBytes(password);
                byte[] inArray = HashAlgorithm.Create("SHA1").ComputeHash(bytes);
                return Convert.ToBase64String(inArray);
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}

